home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 8434 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  1.6 KB

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: hex to dec function?
  5. Date: 3 Mar 1996 22:11:20 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4he1i8INNbmc@keats.ugrad.cs.ubc.ca>
  8. References: <4gdh1b$bg@mailhost.mwmicro.com> <4gg3h0INN59e@keats.ugrad.cs.ubc.ca> <danpop.824997504@rscernix> <4hab7t$uo4@news.vcnet.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <4hab7t$uo4@news.vcnet.com>,
  12. Carl Jacobson <swedecj@vcnet.com> wrote:
  13.  >Why does the following hang my pc when the printf statement is being
  14.  
  15. Because your PC's so-called OS is not robust enough to protect it self from an
  16. insignificant little programming bug.
  17.  
  18.  >executed. If I put a breakpoint on the printf, watch and reset,
  19.  >everything seems OK up to that point. The answer appears to be in the
  20.  >above comments, but I do not understand the premise.
  21.  >
  22.  >   char *pref = "0x";
  23.  >   char *suff;
  24.  >   long numb;
  25.  >
  26.  >      scanf("%s",suff);
  27.  
  28. Suff is an unitialized pointer. It points to no valid storage. You could have
  29. declared suff as a character array,  char suff[20];
  30.  
  31. Using "%s" in scanf without a numeric restriction, such as "%19s", is so bad,
  32. you might as well use gets().
  33.  
  34.  >      strcat(pref,suff);
  35.  
  36. This is incorrect. You are trying to write to a string literal, which is
  37. illegal. Furthermore, you are trying to write _past_ the literal, which is 
  38. absolutely taboo.
  39.  
  40.  >      sscanf(pref,"0x%lx",&numb);
  41.  
  42. You don't need to scan the 0x prefix. Why not check a reference like the K&R2
  43. if you are not sure?
  44. -- 
  45.  
  46.